//
//  PublishView.m
//  talkDemo
//
//  Created by Reisen on 5/5/15.
//  Copyright (c) 2015 刘赞黄Telen. All rights reserved.
//

#import "UploadView.h"
#import "Utility.h"
#import "TalkDefine.h"

@implementation UploadView

- (void)uploadProgressPercentage:(int)percentage
{
    TLDebugS(@"Upload progress:%d", percentage);
    
    self.progressView.progress = percentage / 100.0f;
}

-(void) uploadFailed:(id)sender
{
    self.progressView.progress = 1;
    if (_delegate && [_delegate respondsToSelector:@selector(uploadView_uploadedFailed:)]) {
        [_delegate uploadView_uploadedFailed:uploader];
    }
    
    [self removeFromSuperview];
    [[[UIAlertView alloc] initWithTitle:TalkLocalizedString(@"Upload failed.", nil) message:TalkLocalizedString(uploader.message, nil) delegate:nil cancelButtonTitle:TalkLocalizedString(@"OK", nil) otherButtonTitles:nil] show];
}

-(void) uploadCompleted:(id)sender
{
    self.progressView.progress = 1;
    
    if (_delegate && [_delegate respondsToSelector:@selector(uploadView_uploadedSuccess:)]) {
        [_delegate uploadView_uploadedSuccess:uploader];
    }

#if Debug_To_UploadURL
    self.hidden = YES;
    
    if([uploader.status isEqualToString:@"ok"])
    {
        UIAlertView* av = [[UIAlertView alloc] initWithTitle:TalkLocalizedString(@"Video is uploaded.", nil) message:TalkLocalizedString(@"Do you want to see the video?", nil) delegate:self cancelButtonTitle:TalkLocalizedString(@"No", nil) otherButtonTitles:TalkLocalizedString(@"Yes", nil),nil];
        
        [av show];
    }
    else
        [[[UIAlertView alloc] initWithTitle:TalkLocalizedString(@"Upload failed.", nil) message:TalkLocalizedString(uploader.message, nil) delegate:nil cancelButtonTitle:TalkLocalizedString(@"OK", nil) otherButtonTitles:nil] show];
#else
    [self removeFromSuperview];
#endif
}

//================================================

-(id)initWithFrame:(CGRect)frame path:(NSString*)path username:(NSString*)username uId:(long long)uId
{
    self = [super initWithFrame:frame];
    self.userInteractionEnabled = YES;
    
    //Just to disable it from tapping through.
    UITapGestureRecognizer* gesReg = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
    [self addGestureRecognizer:gesReg];
    
    self.backgroundColor = [UIColor colorWithWhite:1 alpha:0.96f];
    
    
    UIImageView* animu = [[UIImageView alloc] init];
    animu.frame = CGRectMake(0, 0, 450 * 0.55f, 560 * 0.55f);
    animu.center = CGPointMake(self.center.x, 360);
    [self addSubview:animu];
    NSMutableArray* animuImages = [NSMutableArray array];
    
    for(int i = 1; i <= 12; i++)
    {
        [animuImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%04d.png", i]]];
    }
    
    animu.animationImages = animuImages;
    animu.animationRepeatCount = 0;
    animu.animationDuration = 2;
    [animu startAnimating];
    
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 98, 300, 2)];
    
    self.progressView.center = CGPointMake(self.center.x, 620);
    [self addSubview:self.progressView];
    self.progressView.transform = CGAffineTransformMakeScale(1, 3);
    self.progressView.tintColor = [UIColor colorWithRed:138/255.0f green:142/255.0f blue:201/255.0f alpha:1];
    
    self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 768, 50)];
    self.statusLabel.font = [UIFont systemFontOfSize:28];
    self.statusLabel.textAlignment = NSTextAlignmentCenter;
    self.statusLabel.text = TalkLocalizedString(@"Uploading...", "");
    self.statusLabel.center = CGPointMake(self.center.x, 560);
    [self addSubview:self.statusLabel];
    
    uploader = [FileUploader new];
    uploader.delegate = self;
    uploader.username = username;
    uploader.uId = uId;
    
    if(uploader.username == nil)
        uploader.username = @"Mister Roboto";
    
    [uploader uploadFile:path];
    
    return self;
}


-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

-(void)uploadProgress:(float) progress
{
    TLDebugS(@"Upload prugress...");
    self.progressView.progress = progress;
}

//Go to URL or not
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self removeFromSuperview];

    TLDebugS(@"Alert!");
    if(buttonIndex != alertView.cancelButtonIndex)
    {
        TLDebugS(@"URL = %@", uploader.url);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:uploader.url]];
    }
}


-(IBAction)back:(id)sender
{
    UINavigationController* rootViewController = (UINavigationController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    [rootViewController popViewControllerAnimated:YES];
}
@end
